﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
namespace DAL
{
    public class MessageDAL
    {
        public bool addMessage(Albon.Model.Message message)
        {
            SqlParameter[] parameters =new SqlParameter[] {
					new SqlParameter("@ID", message.ID),
					new SqlParameter("@orderID", message.orderID),
					new SqlParameter("@refID", message.refID),
					new SqlParameter("@sender", message.sender),
					new SqlParameter("@receiver", message.receiver),
					new SqlParameter("@title", message.title),
					new SqlParameter("@content",message.content),
					new SqlParameter("@isMarked", message.isMarked),
					new SqlParameter("@emailAddress",message.EmailAddress)
            };
            int ret= SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_messageadd", parameters);
            return ret > 0;
        }

        /// <summary>
        /// 根据ID获取信息数据
        /// </summary>
        /// <returns></returns>
        public DataTable getMessage(string limit)
        {
            string cmdstr = "select * from MessageView where "+limit;
            return SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
        }
        //回复信息
        public bool replyMessage(Albon.Model.Message message)
        {
            SqlParameter[] parameters = new SqlParameter[] {
					new SqlParameter("@refID", message.refID),
					new SqlParameter("@sender", message.sender),
                    new SqlParameter("@content",message.content)
            };
            int ret = SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_messagereply", parameters);
            return ret > 0;
        }

        //将该消息设为已读（当收件人是当前登录用户，该信息已被显示，并且该订单已经被Process的，才可设为已读）
        public void readMessage(string id)
        {
            string cmdstr = "update MessageInfo set isRead=1 where ID=" + id;
            SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null);
        }
    }
}
